/**
 * Created by vijanny on 2017-03-01.
 */
'use strict'
var order={
    orderNumber:'',
    tableNumber:'',
    orderList:[]
};


var tcp = require('./tcpServer');




exports.order =function *(next){
    order = this.request.body.order;
    var re;
    if(order.hasOwnProperty('orderNumber')&&order.hasOwnProperty('tableNumber')&&order.hasOwnProperty('orderList')){

        tcp.sendData(order);
        re = {errorCode:0, message:'ok', res: { }}
    }else{
        re = {errorCode:-1, message: 'error',res:{ }}
    }
    this.status=200;
    this.body=re;
    console.log('a here');
    yield next;
}
exports.reset = function *(next){
    tcp.resetOrder();
    this.status=200;
    this.body={};
}
exports.status =function *(next){
    if(this.request.body.name == 'gmri'&&this.request.body.password == 'admin'){
        this.status=200;
        this.body={err:0,
            redirect:'/devStatus/status.html'
        };
    }else{
        this.status=200;
        this.body={err:-1};
    }
}

exports.info =function *(next){
    var status = tcp.getDevStatus();
    this.status=200;
    this.body=status;
}

var openIH = new Buffer([0x55,0xAA,0xC8,0x00,0x00,0x00,0x00,0x00,0x9F,0x4A]);
var closeIH = new Buffer([0x55,0xAA,0x00,0x00,0x00,0xC8,0x00,0x00,0x0E,0xFC]);
var addIH = new Buffer([0x55,0xAA,0x00,0xC8,0x00,0x00,0x00,0x00,0x6E,0xD2]);
var subIH=new Buffer([0x55,0xAA,0x00,0x00,0xC8,0x00,0x00,0x00,0xB1,0x62]);
exports.IHController= function *(next){
    if(this.request.body.hasOwnProperty('data')){
        var dataCome = this.request.body.data[0];
        console.log('dataCome = '+ JSON.stringify(dataCome));
        switch (dataCome.switchCode){
            case 'K1':
                if(dataCome.value == true){

                    tcp.sendDataIH(openIH)
                }
                break;
            case 'K2':
                if(dataCome.value == true){
                    tcp.sendDataIH(addIH)
                }break;
            case 'K3':
                if(dataCome.value == true){
                    tcp.sendDataIH(subIH)
                }break;
            case 'K4':
                if(dataCome.value == true){
                    tcp.sendDataIH(closeIH)
                }break;
        }
        this.status=200;
        this.body={
            "result": 1,
            "reason": "正确",
            "data": null
        };
    }else{
        this.status=200;
        this.body={
            "result": 0,
            "reason": "失败",
            "data": null
        }
    }
}


exports.getRHootStatus = function *(next){


}

exports.RHootController = function *(next){
    if(this.request.body.hasOwnProperty('data')){
        var dataCome = this.request.body.data[0];
        console.log('dataCome = '+ JSON.stringify(dataCome));
        tcp.sendDataRH(dataCome.switchCode,dataCome.value);
        this.status=200;
        this.body={
            "result": 1,
            "reason": "正确",
            "data": null
        };
    }else{
        this.status=200;
        this.body={
            "result": 0,
            "reason": "失败",
            "data": null
        }
    }
}